//
//  XS_KCZhengshu.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/29.
//

import UIKit
import RxCocoa
import RxSwift

class XS_KCZhengshu: UIView {
    lazy var disposeBag = DisposeBag()
    lazy var modelPS = PublishSubject<(String, String)>()
    lazy var zhengshuNameModelPS = PublishSubject<(String, String)>()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        upView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func upView() {
        backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        let label = UILabel.xsLabel(size: .scale*18, fontName: .medium, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1))
        addSubview(label)
        label.snp.makeConstraints({
            $0.left.equalTo(.scale*16)
            $0.top.equalTo(.scale*8)
        })
        
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.image = UIImage.init(named: "zhengshuBg")
        addSubview(imgV)
        imgV.snp.makeConstraints({
            $0.top.equalTo(label.snp.bottom).offset(.scale*8)
            $0.left.equalTo(.scale*16)
            $0.right.bottom.equalTo(-.scale*16)
            $0.height.equalTo(.scale * 130) // 给个固定高
        })
        
        let zhengshuNameLab = UILabel.xsLabel(size: .scale * 20, fontName: .semibold, color: UIColor(red: 0.93, green: 0.77, blue: 0.59, alpha: 1.0), alignment: .center, lines: 1, text: "证书名称")
        zhengshuNameLab.isHidden = true
        imgV.addSubview(zhengshuNameLab)
        zhengshuNameLab.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.center.equalToSuperview()
        }
        
        zhengshuNameModelPS.subscribe(onNext: { itemName, zhengshuName in
            zhengshuNameLab.isHidden = false
            label.text = itemName
            let attStr = NSMutableAttributedString.init(string: zhengshuName)
            attStr.addAttribute(.kern, value: .scale * 3, range: NSRange.init(location: 0, length: attStr.length))
            zhengshuNameLab.attributedText = attStr
        }).disposed(by: disposeBag)
        
        modelPS.subscribe(onNext: { text, imgUrl in
            label.text = text
            imgV.sd_setImage(with: URL(string: imgUrl)) { img, _, _, _ in
                imgV.snp.remakeConstraints({
                    $0.top.equalTo(label.snp.bottom).offset(.scale*16)
                    $0.left.equalTo(.scale*16)
                    $0.right.bottom.equalTo(-.scale*16)
                    if let size = img?.size, size.width > 0 {
                        $0.height.equalTo(imgV.snp.width).multipliedBy(size.height/size.width)
                    }
                })
            }
        }).disposed(by: disposeBag)
    }
}
